using PrintTool.Models;

namespace PrintTool.Forms;

public partial class PdfFileEditForm : Form
{
    private PdfFile pdfFile;
    private List<string> availablePrinters;

    public PdfFileEditForm(PdfFile pdfFile, List<string> availablePrinters)
    {
        this.pdfFile = pdfFile;
        this.availablePrinters = availablePrinters;
        InitializeComponent();
        LoadPdfFileData();
        SetupEventHandlers();
    }

    private void SetupEventHandlers()
    {
        btnOK.Click += BtnOK_Click;
        btnPreview.Click += BtnPreview_Click;
    }

    private void LoadPdfFileData()
    {
        txtFileName.Text = pdfFile.FileName;
        txtFilePath.Text = pdfFile.FilePath;
        txtPageCount.Text = pdfFile.PageCount.ToString();

        numPrintCopies.Value = pdfFile.PrintCopies;
        chkColorPrint.Checked = pdfFile.IsColorPrint;
        chkSelected.Checked = pdfFile.IsSelected;

        comboPrinter.Items.Clear();
        comboPrinter.Items.AddRange(availablePrinters.ToArray());
        
        if (!string.IsNullOrEmpty(pdfFile.SelectedPrinter))
        {
            comboPrinter.Text = pdfFile.SelectedPrinter;
        }
        else if (availablePrinters.Count > 0)
        {
            comboPrinter.SelectedIndex = 0;
        }
    }

    private void BtnOK_Click(object? sender, EventArgs e)
    {
        // 保存设置
        pdfFile.PrintCopies = (int)numPrintCopies.Value;
        pdfFile.IsColorPrint = chkColorPrint.Checked;
        pdfFile.IsSelected = chkSelected.Checked;
        pdfFile.SelectedPrinter = comboPrinter.Text;
    }

    private void BtnPreview_Click(object? sender, EventArgs e)
    {
        try
        {
            // 使用系统默认程序打开PDF文件进行预览
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
            {
                FileName = pdfFile.FilePath,
                UseShellExecute = true
            });
        }
        catch (Exception ex)
        {
            MessageBox.Show($"无法预览文件: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
} 